package com.itheima.mall.api.sms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.mapper.sms.HomeRecommendSubjectMapper;
import com.itheima.mall.pojo.sms.HomeRecommendSubject;
import com.itheima.mall.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class HomeRecommendSubjectServiceApiImpl implements HomeRecommendSubjectServiceApi{
    @Autowired
    private HomeRecommendSubjectMapper homeRecommendSubjectMapper;
    @Override
    public void recommendSubjectCreate(HomeRecommendSubject homeRecommendSubject) {
        homeRecommendSubjectMapper.insert(homeRecommendSubject);
    }

    @Override
    public Boolean RecommendSubjectDelete(Long[] ids) {
        Boolean flag = true;
        List<HomeRecommendSubject> homeRecommendSubjectList = homeRecommendSubjectMapper.selectBatchIds(Arrays.asList(ids));
        for (HomeRecommendSubject homeRecommendSubject : homeRecommendSubjectList) {
            if(homeRecommendSubject.getRecommendStatus() == 1){
                flag = false;
            }else {
                homeRecommendSubjectMapper.deleteById(homeRecommendSubject.getId());
            }
        }
        return flag;
    }

    @Override
    public PageResult<HomeRecommendSubject> recommendSubjectList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<HomeRecommendSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(recommendStatus!=null,HomeRecommendSubject::getRecommendStatus,recommendStatus);
        queryWrapper.like(StrUtil.isNotEmpty(productName),HomeRecommendSubject::getSubjectName,productName);
        queryWrapper.orderByDesc(HomeRecommendSubject::getSort);
        Page page = homeRecommendSubjectMapper.selectPage(pageInfo, queryWrapper);
        PageResult pageResult = new PageResult<>();
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setPageSize(Convert.toInt(page.getSize()));
        pageResult.setPageNum(Convert.toInt(page.getCurrent()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        pageResult.setList(page.getRecords());
        return pageResult;
    }

    @Override
    public List<HomeRecommendSubject> findRecommendSubjectByIds(Long[] ids) {
        return homeRecommendSubjectMapper.selectBatchIds(Arrays.asList(ids));
    }

    @Override
    public void update(HomeRecommendSubject homeRecommendSubject) {
        homeRecommendSubjectMapper.updateById(homeRecommendSubject);
    }

    @Override
    public HomeRecommendSubject findRecommendSubjectById(Long id) {
        return homeRecommendSubjectMapper.selectById(id);
    }
}
